import json
import logging
import os
import time
from datetime import datetime
import re
import datetime
from Database import Database
from sonyflake import SonyFlake
from baiduspider import BaiduSpider

logging.basicConfig(format='%(asctime)s %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)

db = Database()


def current_time():
    return datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z')


def current_date():
    return datetime.now().astimezone().strftime('%Y-%m-%d')


def ensure_dir(file):
    directory = os.path.abspath(os.path.dirname(file))
    if not os.path.exists(directory):
        os.makedirs(directory)


def strtotime(_str):
    """
    时间格式转时间戳，如：前天12:29、3小时前、昨天22:05
    :param _str:
    :return: int: 时间戳
    """
    _str = str(_str)
    pattern_1 = r"(\d+)小时前"
    pattern_2 = r"(\d+)分钟前"
    pattern_3 = r"昨天(\d+\:\d+)"
    pattern_4 = r"前天(\d+\:\d+)"
    pattern_5 = r"^(?!年)(\d+)月(\d+)日$"
    pattern_6 = r"(\d+)年(\d+)月(\d+)日"
    pattern_7 = r"今天"
    pattern_8 = r"昨天"
    pattern_9 = r"前天"
    pattern_10 = r"(\d+)天前"
    now_time = datetime.datetime.now()
    if re.search(pattern_1, _str):
        hour = int(re.search(pattern_1, _str).group(1))
        return to_unixtime((now_time + datetime.timedelta(hours=-hour)).strftime("%Y-%m-%d %H:%M:00"))
    if re.search(pattern_2, _str):
        minutes = int(re.search(pattern_2, _str).group(1))
        return to_unixtime((now_time + datetime.timedelta(minutes=-minutes)).strftime("%Y-%m-%d %H:%M:00"))
    if re.search(pattern_3, _str):
        timediff = re.search(pattern_3, _str).group(1)
        return to_unixtime((now_time + datetime.timedelta(days=-1)).strftime(f"%Y-%m-%d {timediff}:00"))
    if re.search(pattern_4, _str):
        timediff = re.search(pattern_4, _str).group(1)
        return to_unixtime((now_time + datetime.timedelta(days=-2)).strftime(f"%Y-%m-%d {timediff}:00"))
    if re.search(pattern_5, _str):
        timediff = re.search(pattern_5, _str).groups()
        return to_unixtime(now_time.strftime(f"%Y-{timediff[0]}-{timediff[1]} 00:00:00"))
    if re.search(pattern_6, _str):
        timediff = re.search(pattern_6, _str).groups()
        return to_unixtime(now_time.strftime(f"{timediff[0]}-{timediff[1]}-{timediff[2]} 00:00:00"))
    if re.search(pattern_7, _str):
        return to_unixtime(now_time.strftime(f"%Y-%m-%d 00:00:00"))
    if re.search(pattern_8, _str):
        return to_unixtime((now_time + datetime.timedelta(days=-1)).strftime(f"%Y-%m-%d 00:00:00"))
    if re.search(pattern_9, _str):
        return to_unixtime((now_time + datetime.timedelta(days=-2)).strftime(f"%Y-%m-%d 00:00:00"))
    if re.search(pattern_10, _str):
        daydiff = int(re.search(pattern_10, _str).group(1))
        return to_unixtime((now_time + datetime.timedelta(days=-daydiff)).strftime("%Y-%m-%d 00:00:00"))
    return 0


def to_unixtime(_str):
    return int(time.mktime(time.strptime(_str, '%Y-%m-%d %H:%M:%S')))


def write_text(file: str, text: str):
    ensure_dir(file)
    with open(file, mode='w') as f:
        f.write(text)


def _format(string: str) -> str:
    """去除字符串中不必要的成分并返回

    Args:
        string (str): 要整理的字符串

    Returns:
        str: 处理后的字符串
    """
    text_to_replace = ("\xa0", "\u2002""\u3000")
    string = string.strip()
    for text in text_to_replace:
        string = string.replace(text, "")
    return string


def insert_hot_words(params, hot_words):
    hot_words_tuple = []
    hot_words_list = []
    table_name = 'dc_hot_words'
    for val in hot_words:
        word_title = val['title']
        if db.select_one(table_name, f"source_id={val['source_id']} AND title='{str(word_title)}'"):
            print(f"[{word_title}]已存在,跳过")
            continue
        hot_words_tuple.append(tuple(dict(val).values()))
        hot_words_list.append(val)
    flag = db.insert_many(table=table_name, params=params, all_data=hot_words_tuple)
    if flag:
        return hot_words_list
    return False


def unique_id():
    sf = SonyFlake()
    return sf.next_id()


def domain_match(news_url, domains):
    for val in domains:
        if str(news_url).find(str(val['domain'])) > 0:
            return val['id']
    return 0


def baidu_search(data_list):
    media_domains = db.select_more('dc_co_media')
    baidu_spider = BaiduSpider()
    baidu_news_params = (
        "hot_word_id", "title", "author", "date", "description", "url", "cover", "media_id", "create_time",
        "publish_time")
    baidu_news_list = []
    baidu_news_tuple = []
    hot_words_tbl = "dc_hot_words"
    for word in data_list:
        hot_word_id = str(word['id'])
        print(f"正在从百度新闻里搜索热词:({word['title']})")
        db.update(hot_words_tbl, {"status": 1}, f"id={hot_word_id}")
        response_data = baidu_spider.search_news(query=word['title'])
        response_data = response_data.plain
        create_time = str(int(time.time()))
        for val in response_data:
            news_url = val['url']
            media_id = domain_match(val['url'], media_domains)
            if int(media_id) == 0:
                print(f"{val['url']}不存在合作媒体中，跳过")
                continue
            item = {
                baidu_news_params[0]: hot_word_id,
                baidu_news_params[1]: val['title'],
                baidu_news_params[2]: val['author'],
                baidu_news_params[3]: val['date'],
                baidu_news_params[4]: val['des'],
                baidu_news_params[5]: news_url,
                baidu_news_params[6]: val['cover'],
                baidu_news_params[7]: media_id,
                baidu_news_params[8]: create_time,
                baidu_news_params[9]: strtotime(val['date']),
            }
            print(f"title---{item['title']},date---{item['date']},description---{item['description']}")
            baidu_news_list.append(item)
            baidu_news_tuple.append(tuple(item.values()))
        db.update(hot_words_tbl, {"status": 2}, f"id={hot_word_id}")
        time.sleep(1)
    db.insert_many('dc_baidu_news', baidu_news_params, tuple(baidu_news_tuple))
    print('百度新闻搜索结束并插入数据库成功')
